package com.tencent.txcc.web.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.tencent.txcc.domain.Integral;
import com.tencent.txcc.query.IntegralQuery;
import com.tencent.txcc.result.JSONResult;
import com.tencent.txcc.result.PageList;
import com.tencent.txcc.service.IIntegralService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/integral")
public class IntegralController {

    @Autowired
    public IIntegralService integralService;

    @RequestMapping(value = "/getIntegralByUserId/{user_id}",method = RequestMethod.GET)
    public JSONResult getIntegralByUserId(@PathVariable("user_id")Long user_id){
        return JSONResult.success(integralService.getIntegralByUserId(user_id));
    }


    /**
    * 保存和修改公用的
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public JSONResult saveOrUpdate(@RequestBody Integral integral){
        if(integral.getId()!=null){
            integralService.updateById(integral);
        }else{
            integralService.insert(integral);
        }
        return JSONResult.success();
    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public JSONResult delete(@PathVariable("id") Long id){
        integralService.deleteById(id);
        return JSONResult.success();
    }

    /**
   * 获取对象
   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public JSONResult get(@PathVariable("id")Long id){
        return JSONResult.success(integralService.selectById(id));
    }


    /**
    * 查询所有对象
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JSONResult list(){
        return JSONResult.success(integralService.selectList(null));
    }


    /**
    * 带条件分页查询数据
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public JSONResult page(@RequestBody IntegralQuery query){
        Page<Integral> page = new Page<Integral>(query.getPage(),query.getRows());
        page = integralService.selectPage(page);
        return JSONResult.success(new PageList<Integral>(page.getTotal(),page.getRecords()));
    }
}
